package com.ygqh.baby.service.weixin.token;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.model.Token;
import com.foxinmy.weixin4j.model.WeixinAccount;
import com.foxinmy.weixin4j.mp.token.WeixinTokenCreator;
import com.foxinmy.weixin4j.token.TokenHolder;
import com.ygqh.baby.service.WxTokenStorager;

@Component
public class WxTokenManager {

	protected final Logger logger = Logger.getLogger(this.getClass());
	@Autowired
	private WxTokenStorager tokenStorager;
	@Value("${image.domain}")
	private String imageDomain;

	public TokenHolder getTokenHolder(WeixinAccount weixinAccount) {
		WeixinTokenCreator weixinTokenCreator = new WeixinTokenCreator(weixinAccount.getId(), weixinAccount.getSecret());
		TokenHolder tokenHolder = new TokenHolder(weixinTokenCreator, tokenStorager);
		return tokenHolder;
	}
	public TokenHolder getTokenHolder(String appId, String appSecret) {
		WeixinTokenCreator weixinTokenCreator = new WeixinTokenCreator(appId, appSecret);
		TokenHolder tokenHolder = new TokenHolder(weixinTokenCreator, tokenStorager);
		return tokenHolder;
	}

	public Token getToken(String appId, String appSecret) throws WeixinException {
		TokenHolder tokenHolder = this.getTokenHolder(appId, appSecret);
		return tokenHolder.getToken();
	}

	public String getAccessToken(String appId, String appSecret) throws WeixinException {
		Token token = this.getToken(appId, appSecret);
		return token.getAccessToken();
	}

	public Token resetToken(String appId, String appSecret) throws WeixinException {
		TokenHolder tokenHolder = this.getTokenHolder(appId, appSecret);
		return tokenHolder.refreshToken();
	}

}
